<template>
	<el-container>
		<el-aside width="35%">
			<el-container>
				<el-header>
					<div class="left-panel">
						<el-button icon="el-icon-plus" type="primary" @click="open_dialog" />
					</div>
				</el-header>
				<el-main class="nopadding">
					<scTable
						ref="table"
						:api-obj="this.$API.procrouting.page"
						row-key="id"
						paginationLayout="prev, pager, next"
						highlightCurrentRow
						hideDo
						hideContextMenu
						@row-click="rowClick">
						<el-table-column label="#" type="index" align="center" width="20"></el-table-column>
						<el-table-column property="routingCode" label="路线编号" align="center" width="100" />
						<el-table-column property="routingName" label="路线名称" align="center" />
						<el-table-column property="lineCode" label="产线编号" align="center" width="100" />
						<el-table-column align="center" label="操作" width="180">
							<template #default="scope">
								<el-button text type="primary" size="small" @click="open_dialog(scope.row)"
									>编辑</el-button
								>
								<el-divider direction="vertical" />
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="primary" size="small">删除</el-button>
									</template>
								</el-popconfirm>
							</template>
						</el-table-column>
					</scTable>
				</el-main>

				<modify ref="modify" @complete="complete" />
			</el-container>
		</el-aside>

		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button
						icon="el-icon-plus"
						type="primary"
						:disabled="(this.selectedParam.routingId || 0) == 0"
						@click="child_open_dialog" />
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable
					ref="childTable"
					:api-obj="this.$API.procroutingitem.pageByRoutingId"
					row-key="id"
					:params="selectedParam"
					:initData="false"
					hideDo
					hideContextMenu>
					<el-table-column label="#" type="index" width="50" />
					<el-table-column property="process.procCode" label="工序编号" align="center" width="100" />
					<el-table-column property="process.procName" label="工序名称" align="center" width="100" />
					<el-table-column property="process.procTypeName" label="工艺类型" align="center" width="100" />
					<el-table-column property="pos" label="排序" align="center" width="100">
						<template #default="scope">
							<el-link style="color: #5cd29d" :underline="false" @click="upSort(scope.row)">
								<el-icon><el-icon-top /></el-icon>
							</el-link>
							<el-divider direction="vertical" />
							<el-link style="color: #5cd29d" :underline="false" @click="downSort(scope.row)">
								<el-icon><el-icon-bottom /></el-icon>
							</el-link>
						</template>
					</el-table-column>
					<el-table-column property="ctlAttr" label="工序管控" align="center" width="100">
						<template #default="scope">
							<el-tag :type="ctlAttrTagType(scope.row.ctlAttr)">{{ scope.row.ctlAttrDesc }}</el-tag>
						</template>
					</el-table-column>
					<el-table-column
						property="policyCheckPrevProcDesc"
						label="校验上一工序"
						align="center"
						width="120" />
					<el-table-column property="policyCheckProcBomDesc" label="校验生产BOM" align="center" width="120" />
					<el-table-column property="policyCheckProcess" label="工艺校验策略" align="center" width="120">
						<template #default="scope">
							<el-popover placement="left" :width="220" trigger="click">
								<template #reference>
									<el-button size="small" :disabled="!scope.row.policyCheckProcessChecked.length">
										详细
									</el-button>
								</template>
								<el-table :data="scope.row.policyCheckProcessChecked" :show-header="false">
									<el-table-column width="200" property="label" label="策略" />
								</el-table>
							</el-popover>
						</template>
					</el-table-column>
					<el-table-column property="stdOpDuration" label="标准加工工时（秒）" align="center" width="140" />
					<el-table-column property="stdPrepDuration" label="标准准备工时（秒）" align="center" width="140" />
					<el-table-column property="isDisabled" label="是否禁用" align="center" width="80">
						<template #default="scope">
							<el-tag disable-transitions :type="scope.row.isDisabled ? 'danger' : 'success'">
								{{ scope.row.isDisabled ? "禁用" : "启用" }}
							</el-tag>
						</template>
					</el-table-column>
					<el-table-column property="createTime" label="创建时间" align="center" width="140" />
					<el-table-column align="center" fixed="right" label="操作" width="140">
						<template #default="scope">
							<el-button text type="primary" size="small" @click="child_open_dialog(scope.row)">
								编辑
							</el-button>
							<el-divider direction="vertical" />
							<el-popconfirm title="确定删除吗？" @confirm="childTable_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</template>
					</el-table-column>
				</scTable>
			</el-main>

			<proc-selector ref="procselector" @complete="selectComplete" />
		</el-container>
	</el-container>

	<product-selector ref="productselector" />
</template>

<script>
import modify from "./modify";
import procSelector from "./procSelector";
import productSelector from "./productSelector";

export default {
	components: {
		modify,
		procSelector,
		productSelector,
	},
	data() {
		return {
			selectedParam: { routingId: 0, routingCode: '', lineId: 0, lineCode: '' },
		};
	},
	methods: {
		complete() {
			this.$refs.table.refresh();
		},
		selectComplete() {
			this.$refs.childTable.refresh();
		},
		ctlAttrTagType(item) {
			switch (item) {
				case 1: return '';
				case 2: return 'warning';
				default: return '';
			}
		},
		rowClick(row) {
			this.selectedParam.routingId = row.id;
			this.selectedParam.routingCode = row.routingCode;
			this.selectedParam.lineId = row.lineId;
			this.selectedParam.lineCode = row.lineCode;
			this.$refs.childTable.reload(this.selectedParam);
		},
		async upSort(row) {
			if (row.pos == 1) {
				return;
			}

			await this.$API.procroutingitem.upsort.put(row);
			this.$refs.childTable.refresh();
		},
		async downSort(row) {
			await this.$API.procroutingitem.downsort.put(row);
			this.$refs.childTable.refresh();
		},
		async table_del(row) {
			const resp = await this.$API.procrouting.delete.delete([row.id]);
			if (resp.code == 200) {
				this.$refs.table.refresh();
				this.$message.success("删除成功");
			} else {
				this.$alert(resp.message, "提示", { type: "error" });
			}
		},
		open_dialog(row) {
			if (row.id) {
				this.$refs.modify.open(row);
			} else {
				this.$refs.modify.open();
			}
		},
		openProductSelector(row) {
			this.$refs.productselector.open(row);
		},
		async childTable_del(row) {
			const resp = await this.$API.procroutingitem.delete.delete(row.id);
			if (resp.code == 200) {
				this.$refs.childTable.refresh();
				this.$message.success("删除成功");
			} else {
				this.$alert(resp.message, "提示", { type: "error" });
			}
		},
		child_open_dialog(row) {
			if (row.id) {
				this.$refs.procselector.open(row, this.selectedParam);
			} else {
				this.$refs.procselector.open(null, this.selectedParam);
			}
		},
	},
};
</script>
